import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import Admin from '../components/Admin.vue'
import Message from '../components/Message.vue'
import SetUp from '../components/SetUp.vue'
import Upload from '../components/Upload.vue'
import Resources from '../components/Resources.vue'
import Edit from '../components/Journalism/Edit.vue'
import Journalism from '../components/Journalism/Journalism.vue'
import Alter from '../components/Journalism/Alter.vue'
import Index from '../components/Index.vue'
import TimeTable from '../components/TimeTable.vue'


Vue.use(VueRouter)

const router =  new VueRouter({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    { 
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/admin', component: Admin },
        { path: '/message', component: Message },
        { path: '/setup', component: SetUp },
        { path: '/upload', component: Upload },
        { path: '/resources', component: Resources },
        { path: '/edit', component: Edit },
        { path: '/journalism', component: Journalism },
        { path: '/alter', component: Alter },
        { path: '/index', component: Index },
        { path: '/timeTable', component: TimeTable },
      ]},
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to:将要访问的路径
  // from:从哪里访问的路径
  // next:之后要做的任务，是一个函数
  //    next（）放行， next（'/URL'）强制跳转的路径。
  if (to.path == '/login') return next();// 访问路径为登录
  // 获取flag
  const flagStr = window.sessionStorage.getItem("acess");// session取值
  if (!flagStr) return next('/login');// 没登录去登录
  next();
})

export default router// 暴露出去